//
//  TSLimitConvertHeaderView.swift
//  TSProfitProject
//
//  Created by TmmmS on 2019/11/5.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

class TSLimitConvertHeaderView: UIView {

    private var avatar : UIImageView!
    private var nameLabel : UILabel!
    private var identifyLabel : UILabel!
    private var storeItem : TSWithDrawItem!
    private var profitItem : TSWithDrawItem!
    private var gainItem : TSWithDrawItem!

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = WHITE_COLOR
        configViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func refreshHeaaerView() {
        
        profitItem.setupView(price: String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.yingli ?? 0))
        
        gainItem.setupView(price: String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.yongjin ?? 0))
        
        storeItem.setupView(price: String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.zhongxin ?? 0))
    }
    
    private func configViews() {
        
        let containerView = UIView.init()
        containerView.backgroundColor = BASE_PURPLE_COLOR
        containerView.layer.cornerRadius = 10
        containerView.layer.masksToBounds = true
        self.addSubview(containerView)
        containerView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(17)
            make.width.equalTo(SCREEN_WIDTH - 2*12)
            make.height.equalTo(191)
        }
        
        avatar = UIImageView.init()
        avatar.image = UIImage(named: "general_avatar_default")
        avatar.backgroundColor = WHITE_COLOR
        avatar.layer.cornerRadius = 30
        avatar.layer.masksToBounds = true
        containerView.addSubview(avatar)
        avatar.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(15)
            make.size.equalTo(CGSize(width: 60, height: 60))
        }
        
        nameLabel = UILabel.init()
        nameLabel.text = TSLoginManager.shared._usermodel?.username ?? "暂未设置"
        nameLabel.textColor = WHITE_COLOR
        nameLabel.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        containerView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(avatar.snp_bottom).offset(5)
        }
        
        identifyLabel = UILabel.init()
        identifyLabel.text = "ID\(TSLoginManager.shared._usermodel?.user_id ?? 0)"
        identifyLabel.textColor = WHITE_COLOR
        identifyLabel.font = UIFont.systemFont(ofSize: 12)
        containerView.addSubview(identifyLabel)
        identifyLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(nameLabel.snp_bottom).offset(8)
        }
        
        let itemContainer = UIView.init()
        itemContainer.backgroundColor = UIColor.clear
        containerView.addSubview(itemContainer)
        itemContainer.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(identifyLabel.snp_bottom).offset(10)
            make.height.equalTo(33)
        }
        
        profitItem = createItem(title: "盈利钱包")
        profitItem.setupView(price: String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.yingli ?? 0))
        itemContainer.addSubview(profitItem)
        
        gainItem = createItem(title: "收益钱包")
        gainItem.setupView(price: String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.yongjin ?? 0))
        itemContainer.addSubview(gainItem)
        
        storeItem = createItem(title: "中心钱包")
        storeItem.setupView(price: String.init(format: "￥%.2f", TSLoginManager.shared._usermodel?.zhongxin ?? 0))
        itemContainer.addSubview(storeItem)
        
        let itemArray : [TSWithDrawItem] = [profitItem, gainItem, storeItem]
        itemArray.snp.distributeViewsAlong(axisType: .horizontal, fixedSpacing: 5, leadSpacing: 0, tailSpacing: 0)
        itemArray.snp.makeConstraints { (make) in
            make.top.height.equalToSuperview()
        }
        
        let line = UIView.init()
        line.backgroundColor = BASE_LINE_COLOR
        self.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }
    
    private func createItem(title:String) -> TSWithDrawItem {
        
        let item = TSWithDrawItem.init(title)
        return item
    }
}
